{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1215: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  a = -(dx2)/(dx1 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1216: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  b = (dx2 - dx1) / (dx1 * dx2)\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1217: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c = dx1 / (dx2 * (dx1 + dx2))\n",
      "d:\\anaconda3\\envs\\py38\\lib\\site-packages\\numpy\\lib\\function_base.py:1223: RuntimeWarning: invalid value encountered in add\n",
      "  out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] + c * f[tuple(slice4)]\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 固定参数\n",
    "W = 1000  # 沟道宽度\n",
    "C_ox = 3.45e-8  # 氧化层电容，假设为常数\n",
    "V_th = 0.7  # 阈值电压，假设为常数\n",
    "\n",
    "# 获取所有前缀为IdVg的CSV文件\n",
    "directory = \"E:\\\\Study\\\\Postgraduate\\\\Zhongshan\\\\experiment_data\\\\ITO-MIS\\\\20241016_ITO-RF90-50-8-600-Al-DC30-50-5-1000\\\\200c1h-180c15m\"\n",
    "file_prefix = \"IdVg\"\n",
    "files = [f for f in os.listdir(directory) if f.startswith(file_prefix) and f.endswith('.csv')]\n",
    "\n",
    "# 创建名为gm的文件夹（如果不存在）\n",
    "output_dir = os.path.join(directory, 'mobility')\n",
    "os.makedirs(output_dir, exist_ok=True)\n",
    "\n",
    "length_map = {\n",
    "    (1, 1): 200, (1, 2): 300, (1, 3): 200, (1, 4): 300,\n",
    "    (2, 1): 400, (2, 2): 500, (2, 3): 400, (2, 4): 500,\n",
    "    (3, 1): 200, (3, 2): 300, (3, 3): 200, (3, 4): 300,\n",
    "    (4, 1): 400, (4, 2): 500, (4, 3): 400, (4, 4): 500,\n",
    "}\n",
    "\n",
    "for file_name in files:\n",
    "    file_path = os.path.join(directory, file_name)\n",
    "    \n",
    "    # 读取CSV文件\n",
    "    df = pd.read_csv(file_path)\n",
    "    \n",
    "    # 提取Vgs和Ids列，并将Ids取绝对值\n",
    "    Vgs = df[\"Vgs\"]\n",
    "    Ids = np.abs(df[\"Ids\"])\n",
    "    Vds = df[\"Vds\"]\n",
    "    \n",
    "    # 计算跨导 (gm) 使用numpy库中的gradient函数\n",
    "    gm = np.gradient(Ids, Vgs)\n",
    "    \n",
    "    # 从文件名中提取沟道长度\n",
    "    # 假设文件名格式为 \"IdVg_RxCy.csv\"，其中 x 和 y 是行列号\n",
    "    parts = file_name.split('_')\n",
    "    row_col = parts[1]\n",
    "    row = int(row_col[1])  # 获取行号\n",
    "    col = int(row_col[3])  # 获取列号\n",
    "    L = length_map.get((row, col), 1500)  # 如果没有匹配到，默认长度为1500\n",
    "    \n",
    "    # 计算迁移率 (mu)\n",
    "    mu = gm * L / (W * C_ox * Vds)\n",
    "    \n",
    "    # 将gm和mu添加到原文件的新列中\n",
    "    df['gm'] = gm\n",
    "    df['mu'] = mu\n",
    "    \n",
    "    # 保存修改后的CSV文件到gm文件夹中\n",
    "    output_file_path = os.path.join(output_dir, f\"{os.path.splitext(file_name)[0]}_m.csv\")\n",
    "    df.to_csv(output_file_path, index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
